package com.liao.badminton.context;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;

import java.util.HashMap;
import java.util.Map;

@SuppressWarnings("deprecation")
public class BaseContextHandler {
    public static final String CONTEXT_KEY_USER_ID = "currentUserId";
    public static final String CURRENT_IP = "currentIP";
    public static final String CURRENT_PORT = "currentPort";
    public static final String CONTEXT_KEY_USER_PHONE = "currentUserPhone";
    public static final String CONTEXT_KEY_USER_NAME = "currentUserName";
    public static final String CONTEXT_KEY_USER_TYPE = "currentUserType";
    public static final String CONTEXT_KEY_LOGIN_CODE = "currentLoginCode";
    public static final String CONTEXT_KEY_OPTION = "currentRequestOption";
    public static final Integer user_type_admin = 0;
    public static final Integer user_type_member = 1;
    public static final String HEADER_TTY = "tty";
    public static final String TTY_ANDROID = "android";
    public static final String TTY_IOS = "iOS";
    public static final String TTY_WEB = "web";
    public static final String TTY_H5 = "H5";
    private static final Logger logger = LoggerFactory.getLogger(BaseContextHandler.class);
    public static InheritableThreadLocal<Map<String, Object>> threadLocal = new InheritableThreadLocal<Map<String, Object>>();
    public static ApplicationContext applicationContext;
    public static String env = "dev";


    public static void set(String key, Object value) {
        Map<String, Object> map = threadLocal.get();
        if (map == null) {
            map = new HashMap<String, Object>();
            threadLocal.set(map);
        }
        map.put(key, value);
    }

    public static Object get(String key) {
        Map<String, Object> map = threadLocal.get();
        if (map == null) {
            map = new HashMap<String, Object>();
            threadLocal.set(map);
        }
        return map.get(key);
    }

    public static Long getCurrUserID() {
        String userId = getUserID();
        if (userId == null) {
            return null;
        }
        return Long.parseLong(userId);
    }

    public static String getOption() {
        Object value = get(CONTEXT_KEY_OPTION);
        String option = returnObjectValue(value);
        return option;
    }
    public static String getUserID() {
        Object value = get(CONTEXT_KEY_USER_ID);
        String userId = returnObjectValue(value);
        logger.info("getUserID=" + userId);
        return userId;
    }

    public static String getUserPhone() {
        Object value = get(CONTEXT_KEY_USER_PHONE);
        return returnObjectValue(value);
    }

    public static String getUserName() {
        Object value = get(CONTEXT_KEY_USER_NAME);
        return returnObjectValue(value);
    }

    public static String getUserType() {
        Object value = get(CONTEXT_KEY_USER_TYPE);
        return returnObjectValue(value);
    }

    public static String getLoginCode() {
        Object value = get(CONTEXT_KEY_LOGIN_CODE);
        return returnObjectValue(value);
    }

    /**
     * 访问终端
     *
     * @return
     */
    public static String getTty() {
        Object value = get(HEADER_TTY);
        return returnObjectValue(value);
    }

    public static String getCurrentIp() {
        Object value = get(CURRENT_IP);
        return returnObjectValue(value);
    }
    public static String getCurrentPort() {
        Object value = get(CURRENT_PORT);
        return returnObjectValue(value);
    }


    public static void setLoginCode(String loginCode) {
        set(CONTEXT_KEY_LOGIN_CODE, loginCode);
    }

    public static void setUserID(Long userID) {
        set(CONTEXT_KEY_USER_ID, userID);
    }

    public static void setUserPhone(String userPhone) {
        set(CONTEXT_KEY_USER_PHONE, userPhone);
    }

    public static void setUserName(String userName) {
        set(CONTEXT_KEY_USER_NAME, userName);
    }

    public static void setUserType(Integer userType) {
        set(CONTEXT_KEY_USER_TYPE, userType);
    }

    public static void setOption(String option) {
        set(CONTEXT_KEY_OPTION, option);
    }
    public static void setIp(String ip) {
        set(CURRENT_IP, ip);
    }

    private static String returnObjectValue(Object value) {
        return value == null ? null : value.toString();
    }

    public static void remove() {
        threadLocal.remove();
    }

    public static void setPort(String clientPort) {
        set(CURRENT_PORT, clientPort);
    }
}
